// 模拟前端日志上传测试脚本
const testLogUpload = async () => {
  console.log('开始测试前端日志上传...');
  
  try {
    // 准备测试日志数据
    const logData = {
      logs: [
        {
          id: Date.now() + Math.random().toString(36).substr(2, 9),
          timestamp: new Date().toISOString(),
          module: 'app',
          level: 'ERROR',
          message: '这是一个测试错误日志 - 模拟前端错误',
          data: {
            error: 'TestError',
            stack: 'Error: 这是一个测试错误\n    at testFunction (test.js:12:15)',
            details: '这是测试错误的详细信息'
          },
          userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
          screen: '1920x1080',
          url: 'http://localhost:8080/system/dashboard',
          userId: 'admin'
        },
        {
          id: Date.now() + Math.random().toString(36).substr(2, 9),
          timestamp: new Date().toISOString(),
          module: 'api',
          level: 'FATAL',
          message: '这是一个测试致命错误日志 - 模拟API调用失败',
          data: {
            api: '/api/test/endpoint',
            method: 'GET',
            status: 500,
            error: 'Internal Server Error'
          },
          userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
          screen: '1920x1080',
          url: 'http://localhost:8080/system/settings',
          userId: 'admin'
        }
      ],
      timestamp: Date.now(),
      environment: 'development',
      clientIp: '127.0.0.1',
      appVersion: '1.0.0'
    };
    
    console.log('准备发送测试日志数据...');
    console.log('日志数量:', logData.logs.length);
    
    // 发送POST请求到日志收集接口
    const response = await fetch('http://localhost:8080/api/logs/collect', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer test-token' // 模拟token
      },
      body: JSON.stringify(logData)
    });
    
    // 检查响应状态
    if (response.ok) {
      console.log('✅ 日志上传成功!');
      console.log('请检查后端logs文件夹中的error.log文件查看是否有记录的错误日志');
    } else {
      console.error('❌ 日志上传失败:', response.status, response.statusText);
      const errorData = await response.json().catch(() => ({}));
      console.error('错误详情:', errorData);
    }
  } catch (error) {
    console.error('❌ 测试过程中发生错误:', error);
  }
};

// 执行测试
console.log('测试脚本已启动');
testLogUpload();